
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;
using AnovoAndes.Framework.Utilidades;
using NovaSystem.EntidadesNegocio;

namespace NovaSystem.AccesoDatos
{
	public class menuDAO 
	{
			
		#region [Variables Publicas]
        List<string> listaEsquema = new List<string>();
		System.Configuration.AppSettingsReader read = new System.Configuration.AppSettingsReader();
        #endregion
		
		#region Constructores
		public menuDAO()
		{
		}
		#endregion
		
		#region Metodos Privados
		
		 private List<string> LlenarEsquema(IDataReader dr)
        {
            DataTable schemaTable;
            schemaTable = dr.GetSchemaTable();
            listaEsquema = new List<string>();
            const int columnaNombre = 0;
            //Para cada campo de la tabla...
            foreach (DataRow myField in schemaTable.Rows)
            {
                //Agrega nombre del campo a la lista generica.
                listaEsquema.Add(myField[columnaNombre].ToString());
            }
            return listaEsquema;

        }
		
		private menu Crear(IDataReader dr)
		{
			menu vObj = new menu();
			try
			{
				listaEsquema.Sort();
							if (listaEsquema.BinarySearch("menuId",StringComparer.OrdinalIgnoreCase)>-1)
				{
					if(!(dr["menuId"] is System.DBNull))
					{
						vObj.menuId= (int)dr["menuId"];
					}
				}
				if (listaEsquema.BinarySearch("descripcion",StringComparer.OrdinalIgnoreCase)>-1)
				{
					if(!(dr["descripcion"] is System.DBNull))
					{
						vObj.descripcion= (string)dr["descripcion"];
					}
				}
				if (listaEsquema.BinarySearch("padreId",StringComparer.OrdinalIgnoreCase)>-1)
				{
					if(!(dr["padreId"] is System.DBNull))
					{
						vObj.padreId= (int)dr["padreId"];
					}
				}
				if (listaEsquema.BinarySearch("posicion",StringComparer.OrdinalIgnoreCase)>-1)
				{
					if(!(dr["posicion"] is System.DBNull))
					{
						vObj.posicion= (int)dr["posicion"];
					}
				}
				if (listaEsquema.BinarySearch("icono",StringComparer.OrdinalIgnoreCase)>-1)
				{
					if(!(dr["icono"] is System.DBNull))
					{
						vObj.icono= (string)dr["icono"];
					}
				}
				if (listaEsquema.BinarySearch("habilitado",StringComparer.OrdinalIgnoreCase)>-1)
				{
					if(!(dr["habilitado"] is System.DBNull))
					{
						vObj.habilitado= (bool)dr["habilitado"];
					}
				}
				if (listaEsquema.BinarySearch("url",StringComparer.OrdinalIgnoreCase)>-1)
				{
					if(!(dr["url"] is System.DBNull))
					{
						vObj.url= (string)dr["url"];
					}
				}
                if (listaEsquema.BinarySearch("parametros", StringComparer.OrdinalIgnoreCase) > -1)
                {
                    if (!(dr["parametros"] is System.DBNull))
                    {
                        vObj.parametros = (string)dr["parametros"];
                    }
                }


				return vObj;
			}
			catch(Exception ex)
			{
				throw new Exception(ex.Message);
			}
		}
		
		private List<menu> CrearDTOs(IDataReader dr) 
		{
			List<menu> listaDTO = new List<menu>();
			if 	(dr != null)
			{
				listaEsquema = LlenarEsquema(dr);// Validadores.LlenarEsquema(dr);
				
				while (dr.Read())
				{
					listaDTO.Add(Crear(dr));
				}
				dr.Close();
			}
			return listaDTO;
		}
		
		#endregion
		
		#region Metodos Publicos
		public List<menu> Listar()
		{
			MySqlConnection conn = new MySqlConnection();
			List<menu> lista = new List<menu>();
			try
			{
				string connString = read.GetValue("Database",typeof(string)).ToString();
				conn.ConnectionString = connString;
				conn.Open();
				MySqlCommand objDbCommand = new MySqlCommand();
				objDbCommand.Connection = conn;
				objDbCommand.CommandType = CommandType.StoredProcedure;
                objDbCommand.CommandText = "usp_S_menu_All";
				
				using (IDataReader objDataReader = objDbCommand.ExecuteReader())
				{
					lista = CrearDTOs(objDataReader);
				}
				CerrarConn(conn);
				return lista;
			}
			catch(Exception ex)
			{
				CerrarConn(conn);
				throw new Exception(ex.Message);
			}
		}
		
		
		private void CerrarConn(MySqlConnection conn)
        {
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
                conn.Dispose();
            }
        }
		
		public string Insertar( menu dto)
		{
			MySqlConnection conn = new MySqlConnection();
			try
			{
				string connString = read.GetValue("Database",typeof(string)).ToString();
				conn.ConnectionString = connString;
				conn.Open();
				MySqlCommand objDbCommand = new MySqlCommand();
				objDbCommand.Connection = conn;
				objDbCommand.CommandType = CommandType.StoredProcedure;
				objDbCommand.CommandText = "usp_I_menu";
				objDbCommand.Parameters.AddWithValue("_menuId",AssertNull.getValue(dto.menuId));
				objDbCommand.Parameters.AddWithValue("_descripcion",AssertNull.getValue(dto.descripcion));
				objDbCommand.Parameters.AddWithValue("_padreId",AssertNull.getValue(dto.padreId));
				objDbCommand.Parameters.AddWithValue("_posicion",AssertNull.getValue(dto.posicion));
				objDbCommand.Parameters.AddWithValue("_icono",AssertNull.getValue(dto.icono));
				objDbCommand.Parameters.AddWithValue("_habilitado",AssertNull.getValue(dto.habilitado));
				objDbCommand.Parameters.AddWithValue("_url",AssertNull.getValue(dto.url));

				objDbCommand.ExecuteNonQuery();
				CerrarConn(conn);
				return "";
			}
			catch(Exception ex)
			{
				CerrarConn(conn);
				throw new Exception(ex.Message);
			}
		}
		
		public void Eliminar(menu dto)
		{
			MySqlConnection conn = new MySqlConnection();
			try
			{
				string connString = read.GetValue("Database",typeof(string)).ToString();
				conn.ConnectionString = connString;
				conn.Open();
				MySqlCommand objDbCommand = new MySqlCommand();
				objDbCommand.Connection = conn;
				objDbCommand.CommandType = CommandType.StoredProcedure;
				objDbCommand.CommandText = "usp_D_menu";
								objDbCommand.Parameters.AddWithValue("_menuId",AssertNull.getValue(dto.menuId));

				objDbCommand.ExecuteNonQuery();
				CerrarConn(conn);
			}
			catch(Exception ex)
			{
				CerrarConn(conn);
				throw new Exception(ex.Message);
			}
		}
		
		#endregion
		
	}
}
